package com.chhuang.huaao.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.huaao.bean.HaLogQueryBean;
import com.chhuang.huaao.mapper.HaLogMapper;
import com.chhuang.huaao.po.HaLog;
import com.chhuang.huaao.po.HaLogExample;
import com.chhuang.huaao.service.IHaLogService;
import com.chhuang.utils.Assert;

/**
* HaLog ServiceImpl
* @author ChHuang
* @date Sun Apr 30 20:19:00 CST 2017
*/
@Service
public class HaLogServiceImpl extends BaseServiceImpl<HaLogMapper, HaLog> implements IHaLogService {
	@Autowired
	private HaLogMapper haLogMapper;

	@Override
	public QueryBean<HaLog> seniorQuery(QueryBean<HaLog> queryBean) throws Exception {
		HaLogQueryBean poQueryBean = (HaLogQueryBean)queryBean;
		HaLogExample example = new HaLogExample();
		
		if(poQueryBean.isAnd()){//全部是与条件
			HaLogExample.Criteria criteria = example.createCriteria();
			if(Assert.isValidString(poQueryBean.getLogId())){
				criteria.andLogIdEqualTo(poQueryBean.getLogId());
			}
			if(Assert.isValidString(poQueryBean.getLogDesc())){
				criteria.andLogDescLike("%"+poQueryBean.getLogDesc()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineId())){
				criteria.andMachineIdLike("%"+poQueryBean.getMachineId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineNum())){
				criteria.andMachineNumLike("%"+poQueryBean.getMachineNum()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				criteria.andCustomerIdLike("%"+poQueryBean.getCustomerId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerAccount())){
				criteria.andCustomerAccountLike("%"+poQueryBean.getCustomerAccount()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerName())){
				criteria.andCustomerNameLike("%"+poQueryBean.getCustomerName()+"%");
			}
			if(poQueryBean.getCreateTime() != null){
				criteria.andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
		}else{//全部是或条件
			if(Assert.isValidString(poQueryBean.getLogId())){
				example.or().andLogIdEqualTo(poQueryBean.getLogId());
			}
			if(Assert.isValidString(poQueryBean.getLogDesc())){
				example.or().andLogDescLike("%"+poQueryBean.getLogDesc()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineId())){
				example.or().andMachineIdLike("%"+poQueryBean.getMachineId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getMachineNum())){
				example.or().andMachineNumLike("%"+poQueryBean.getMachineNum()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				example.or().andCustomerIdLike("%"+poQueryBean.getCustomerId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerAccount())){
				example.or().andCustomerAccountLike("%"+poQueryBean.getCustomerAccount()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerName())){
				example.or().andCustomerNameLike("%"+poQueryBean.getCustomerName()+"%");
			}
			if(poQueryBean.getCreateTime() != null){
				example.or().andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
		}
		
		poQueryBean.setItemCount(haLogMapper.countByExample(example));//总记录数
		if(poQueryBean.getItemCount()>0){//计算总页数
			poQueryBean.setPageCount(poQueryBean.getItemCount()/poQueryBean.getPageSize()+
					(int)(poQueryBean.getItemCount()%poQueryBean.getPageSize()>0?1:0));
			if(poQueryBean.getPageCount()>=poQueryBean.getPageNum()){
				if(Assert.isValidString(poQueryBean.getOrderBy())){//排序
					example.setOrderByClause(poQueryBean.getOrderBy()+" "+poQueryBean.getOrder());
				}
				//分页
				example.setOffset(poQueryBean.getOffset());
				example.setLimit(poQueryBean.getLimit());
				poQueryBean.setResult(haLogMapper.selectByExample(example));
			}
		}
		
		return poQueryBean;
	}

	@Override
	public void deleteByPrimaryKeys(List<String> list) throws Exception {
		HaLogExample example = new HaLogExample();
		HaLogExample.Criteria criteria = example.createCriteria();
		criteria.andLogIdIn(list);
		haLogMapper.deleteByExample(example);
	}
}